# coding: UTF-8
# Filename : chain_commentpost_api.py
# Company : imedia
# Author : rose
# -*-coding:utf-8 -*-
from api.common.common import *
from api.common import mysql,log,utils,httputils,const,xml_tpl, err_tpl
from django.http import HttpResponse
import chain_commentpost_data_model
import time
from api.points_record import points_record_manage
'''
http://api.imedia.com/app/part/list
'''
		
#details list
def chain_commentpost_list(request):
    #record log((frist action))
    httputils.footprint(request)
    #param
    cus_id = httputils.post(request, 'cus_id')#is customer id,more
    target = httputils.post(request, 'target')#is customer id,more
    target_id = httputils.post(request, 'target_id')#is customer id,more
    score = httputils.post(request, 'score')#is customer id,more
    user_id = httputils.post(request, 'user_id')#is customer id,more
    content = httputils.post(request, 'content')#is customer id,more

    if not utils.is_num(target): target = 1
    if not utils.is_num(user_id): user_id = 1
    if not utils.is_num(target_id): target_id = 1
    if not utils.is_num(cus_id): cus_id = 1 
    #Verification
    if validate(request):
        return validate(request)
    #models
    time_d = time.time()
    sql = '''
    insert into chain_comment (cus_id,target,user_id,target_id,content,score,reply_id,created_at,state,source)
    values
    (%s,%s,%s,%s,%s,%s,%s,%s,%s,1)
    '''
    param = [int(cus_id),int(target),int(user_id),int(target_id),content,score,0,time_d,0]
    rst = mysql.insert(sql,param)
    #评论成功增加积分
    obj_type_dict = {0:1,1:2,2:3}
    if rst:
    	obj_type = 0
    	if obj_type_dict.has_key(int(target)): obj_type = obj_type_dict[int(target)]
    	(rst, points) = points_record_manage.record_css(user_id, cus_id, 'comments', obj_type, 2, int(target_id))
    output = ''
    output += chain_commentpost_data_model.imedia_start 
    output += chain_commentpost_data_model.imedia_body 
    output += chain_commentpost_data_model.imedia_end
    return response(output)
